#include "PrescriptionDatabase.h"

/*----------------------
 *
 * PrescriptionDatabase
 *
 *----------------------*/

PrescriptionDatabase::PrescriptionDatabase()
{
    model = nullptr;

    DatabaseLock lock(this);
    QSqlQuery query;

    // Check if prescriptions table already exists
    query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name = 'prescriptions'");

    // If query returns a value, the table already exists and initialization can be skipped
    if (query.next())
        return;

    // Create table to hold prescription data
    query.exec("CREATE TABLE IF NOT EXISTS prescriptions ("
               "prescriptionId INTEGER PRIMARY KEY AUTOINCREMENT, "
               "patientId INTEGER, "
               "prescribeDate TEXT, "
               "completeDate TEXT, "
               "prescriptionName TEXT, "
               "prescribedBy TEXT, "
               "comments TEXT, "
               "refills INTEGER)");
}

bool PrescriptionDatabase::addPrescription(const Prescription * p, int userId)
{
    DatabaseLock lock(this);
    QSqlQuery query;

    query.prepare("INSERT INTO prescriptions "
                  "(patientId, prescribeDate, completeDate, prescriptionName, "
                  "prescribedBy, comments, refills) "
                  "VALUES (?,?,?,?,?,?,?)");
    query.addBindValue(userId);
    query.addBindValue(QVariant(p->prescribeDate.toString("yyyy-MM-dd")));
    query.addBindValue(QVariant(p->completeDate.toString("yyyy-MM-dd")));
    query.addBindValue(QVariant(p->prescriptionName));
    query.addBindValue(QVariant(p->prescribedBy));
    query.addBindValue(QVariant(p->comments));
    query.addBindValue(p->refills);
    query.exec();

    return true;
}

QList<Prescription> * PrescriptionDatabase::loadPrescriptions(int userId)
{
    prescriptionList.clear();

    DatabaseLock lock(this);
    QSqlQuery query;

    query.prepare("SELECT * FROM prescriptions "
                  "WHERE patientId=:patientId");
    query.addBindValue(userId);
    query.exec();

    while(query.next())
    {
        Prescription p;
        p.prescribeDate = QDate::fromString(
                    query.value("prescribeDate").toString(), "yyyy-MM-dd");
        p.completeDate = QDate::fromString(
                    query.value("completeDate").toString(), "yyyy-MM-dd");
        p.prescriptionName = query.value("prescriptionName").toString();
        p.prescribedBy = query.value("prescribedBy").toString();
        p.comments = query.value("comments").toString();
        p.refills = query.value("refills").toInt();

        prescriptionList.push_back(p);
    }

    if (model != nullptr)
        delete model;

    model = new PrescriptionDatabaseModel();
    model->setSource(&prescriptionList);

    return &prescriptionList;
}

PrescriptionDatabaseModel * PrescriptionDatabase::getModel()
{
    return model;
}

/*---------------------------
 *
 * PrescriptionDatabaseModel
 *
 *---------------------------*/

PrescriptionDatabaseModel::PrescriptionDatabaseModel()
{
    rowNum = 0;
    colNum = 6;

    prescriptions = nullptr;
}

void PrescriptionDatabaseModel::setSource(QList<Prescription> * prescriptionList)
{
    prescriptions = prescriptionList;
    //rowNum = prescriptions->size();
}

int PrescriptionDatabaseModel::rowCount(const QModelIndex &) const
{
    return prescriptions->size();
}

int PrescriptionDatabaseModel::columnCount(const QModelIndex &) const
{
    return colNum;
}

QVariant PrescriptionDatabaseModel::data(const QModelIndex &index, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (index.row() >= prescriptions->size())
        return QVariant();

    const Prescription * prescription = &( prescriptions->at(index.row()) );

    switch(index.column())
    {
        case 0: return QVariant(prescription->prescribeDate);
        case 1: return QVariant(prescription->prescriptionName);
        case 2: return QVariant(prescription->refills);
        case 3: return QVariant(prescription->completeDate);
        case 4: return QVariant(prescription->prescribedBy);
        case 5: return QVariant(prescription->comments);
    }

    return QVariant();
}

QVariant PrescriptionDatabaseModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    QString header = "";

    if (role != Qt::DisplayRole && orientation == Qt::Horizontal)
        return QVariant();

    if (section >= colNum)
        return QVariant();

    switch(section)
    {
        case 0: header = "Prescribed Date"; break;
        case 1: header = "Prescription Name"; break;
        case 2: header = "Refills"; break;
        case 3: header = "Completion Date"; break;
        case 4: header = "Prescribed By"; break;
        case 5: header = "Comments"; break;
    }

    return QVariant(header);
}
